package com.qinghuan.designMode.prePrinciple.ocp;

/**
 * @Auther: qinghuan
 * @Date: 2022/2/21 19:50
 * @Description:开闭原则 1、最重要的原则
 * 2、一个软件实体如类、模块和函数应该对拓展开放（对提供方），对修改关闭（对适用方）。用抽象构建框架，用实现拓展细节
 */
public class Ocp {

    public static void main(String[] args) {
        GraphicEditor graphicEditor = new GraphicEditor();
        graphicEditor.drawShape(new Circle());
    }

}

//这里是一个用于绘图的类【使用方】
class GraphicEditor {
    //接收Shape对象，然后根据type，来绘制不同的图形
    public void drawShape(Shape shape) {
        if (shape.m_type == 1) {
            drawRectangle(shape);
        } else if (shape.m_type == 2) {
            drawCircle(shape);
        } else if (shape.m_type == 3) {
            drawTriangle(shape);
        }
    }

    //绘制矩形
    public void drawRectangle(Shape shape) {
        System.out.println("矩形");
    }

    public void drawCircle(Shape shape) {
        System.out.println("圆形");
    }

    public void drawTriangle(Shape shape) {
        System.out.println("三角形");
    }
}


class Shape {
    int m_type;
}

class Circle extends Shape {
    Circle() {
        super.m_type = 1;
    }
}

class Rectangle extends Shape {
    Rectangle() {
        super.m_type = 2;
    }
}

class Triangle extends Shape {
    Triangle() {
        super.m_type = 3;
    }
}